package com.bdqn.gulimall.cart.vo;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * Cart
 * 购物车所有数据
 * 需要计算的属性，必须重写它的get方法，保证每次获取的属性都会进行计算
 * @author f
 * @since 2022/3/1
 */
public class Cart implements Serializable {
    List<CartItem> items;//所有购物项

    private Integer countNum;//商品数量

    private Integer countType;//商品的类型数量

    private BigDecimal totalAmount;//商品总价

    private BigDecimal reduce = new BigDecimal("0.00");//减免价格，默认无减免

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        int count = 0;//商品总数初始为0
        //遍历所有商品
        if (items != null && items.size() != 0){
            for (CartItem item : items) {
                count += item.getCount();
            }
        }
        return countNum;
    }



    public Integer getCountType() {
        int count = 0;//商品总数初始为0
        //遍历所有商品
        if (items != null && items.size() != 0){
            for (CartItem item : items) {
                count += 1;
            }
        }
        return count;
    }



    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        int count = 0;//商品总数初始为0
        //1.遍历所有商品 计算购物项总价
        if (items != null && items.size() != 0){
            for (CartItem item : items) {
                BigDecimal totalPrice = item.getTotalPrice();//计算当前购物项总价的方法
                amount = amount.add(totalPrice);
            }
        }

        //2.减去优惠总价 subtract()方法：减法
        BigDecimal subtract = amount.subtract(getReduce());
        return subtract;
    }



    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
